@using si.caha.Repository;
@using si.caha.Model;
@using System.Web.Script.Serialization;

@{
    if (!WebSecurity.IsAuthenticated)
    {
        Response.Redirect("~/Account/Login");
    }
    
    var firstname = "";
    var surname = "";
    var street = "";
    var postNumber = 0;
    var post = "";
    var country = "";
    
    var response = new Dictionary<string, string>();
    if (IsPost)
    {
        firstname = Request["firstname"];
        surname = Request["surname"];
        street = Request["street"];
        postNumber = Request["postNumber"].AsInt();
        post = Request["post"];
        country = Request["country"];
        
        var dataContext = new DataContext();
        var addressRepository = new AddressRepository(dataContext);
        var address = new Address();
        
        address.Firstname = firstname;
        address.Surname = surname;
        address.StreetWithHouseNumber = street;
        address.Post = post;
        address.PostNumber = postNumber;
        address.Country = country;
        address.UserId = WebSecurity.CurrentUserId;
        
        response = addressRepository.Save(address);
        
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        string json = serializer.Serialize((object)response);
        
        Response.Write(json);
    }
}
